<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Person(name) {
            this.name = name;
        }

        Person.prototype.sayHello = function () {   //在Person的原型对象上面添加sayHello函数
            console.log(`Hello, my name is ${this.name}`);
        };

        function Student(name, major) {
            Person.call(this, name);        //this:john
            this.major = major;
        }

        Student.prototype = Object.create(Person.prototype); //修改Student原型对象为（创建一个新对象为Person的原型对象）
        Student.prototype.constructor = Student;   //指定constructor指向Student
        Student.prototype.sayMajor = function () {  //在Student的原型对象上面添加sayMajor函数
            console.log(`My major is ${this.major}`);
        };

        const john = new Student("John", "Computer Science");
        // 1.请解释这段代码的作用。
        // 2.john能够调用哪些方法？
        //   Person.prototype.sayHello      Student.prototype.sayMajor
        // 3.如果我们在Student的原型对象上添tudent,加一个名为sayHello的方法，会发生什么？
        // 函数名重复，会覆盖
    </script>
</body>

</html>